/**
 * Copyright (c) 2011 Tomi Paananen.
 *
 * Part of Quick Gestures test tool.
 */

#include "setexaminer.h"

// Qt includes
#include <QDateTime>
#include <QFile>
#include <QPoint>
#include <QString>
#include <QTextStream>

// qgengine includes
#include <gesture.h>
#include <qgutils.h>


/*!
  \class SetExaminer
  \brief TODO
*/


/*!
  Constructor.
*/
SetExaminer::SetExaminer()
    : m_gestureCount(0),
      m_totalPointCount(0),
      m_totalGestureWidth(0),
      m_totalGestureHeight(0)
{
}


/*!
  Destructor.
*/
SetExaminer::~SetExaminer()
{
}


/*!
  Clears the statistical data.
*/
void SetExaminer::clear()
{
    m_gestureCount = 0;
    m_totalPointCount = 0;
    m_totalGestureWidth = 0;
    m_totalGestureHeight = 0;
}


/*!
*/
bool SetExaminer::exportAsHTML(const QString &filePath,
                               const QList<Gesture*> &gestureSet,
                               const QString &gestureSetId,
                               bool append /* = false */)
{
    QFile file(filePath);

    QFile::OpenMode openMode;

    if (append) {
        openMode = QIODevice::Append | QIODevice::Text;
    }
    else {
        openMode = QIODevice::WriteOnly | QIODevice::Text;
    }

    if (!file.open(openMode)) {
        return false;
    }

    QTextStream textStream(&file);

    if (!append) {
        textStream << "<p>Date: " << QDateTime::currentDateTime().toString()
                   << "</p>\n";
    }

    // Write meta data.
    textStream << "<p>\nGesture set: " << gestureSetId << "<br />\n"
               << "- gesture count: " << gestureSet.count() << "\n</p>\n";

    // Write the top header row.
    textStream << "<table>\n<tr><th>Index</th><th>ID</th><th>Name</th>"
               << "<th>Points</th><th>Point count</th><th>Width</th>"
               << "<th>Height</th></tr>\n";

    Gesture *gesture(0);
    QPoint point;
    int gestureCount(0);
    int totalPointCount(0);
    int totalWidth(0);
    int totalHeight(0);

    for (int i = 0; i < gestureSet.count(); ++i) {
        gesture = gestureSet[i];

        if (!gesture) {
            continue;
        }

        textStream << "<tr><td>" << i << "</td><td>" << gesture->id()
                   << "</td><td>" << gesture->name() << "</td><td>";

        for (int j = 0; j < gesture->pointCount(); ++j) {
            point = gesture->points().at(j);
            textStream << "[" << point.x() << ", " << point.y() << "]";

            if (j < gesture->pointCount() - 1) {
                textStream << ", ";
            }
        }

        textStream << "</td><td>" << gesture->pointCount() << "</td><td>";

        int minX, maxX, minY, maxY;
        QGUtils::getMinMaxXY(gesture->points(), minX, minY, maxX, maxY);
        textStream << maxX - minX << "</td><td>" << maxY - minY << "</td></tr>\n";

        // Store figures.
        gestureCount++;
        totalPointCount += gesture->pointCount();
        totalWidth += (maxX - minX);
        totalHeight += (maxY - minY);
    }

    // Write average figures row.
    textStream << "<tr><th>Average</th><td></td><td></td><td></td><td>"
               << totalPointCount / gestureCount << "</td><td>"
               << totalWidth / gestureCount << "</td><td>"
               << totalHeight / gestureCount << "</td></tr>\n"
               << "</table>\n";

    file.close();

    // Store total figures.
    m_gestureCount += gestureCount;
    m_totalPointCount += totalPointCount;
    m_totalGestureWidth += totalWidth;
    m_totalGestureHeight += totalHeight;

    return true;
}


/*!
*/
bool SetExaminer::exportTotalFiguresAsHTML(const QString &filePath)
{
    QFile file(filePath);

    if (!file.open(QIODevice::Append | QIODevice::Text)) {
        return false;
    }

    QTextStream textStream(&file);

    textStream << "<table>\n<tr><th>Total gesture count</th>"
               << "<th>Average point count per gesture</th>"
               << "<th>Average gesture width</th>"
               << "<th>Average gesture height</th></tr>\n"
               << "<tr><td>" << m_gestureCount << "</td><td>"
               << m_totalPointCount / m_gestureCount << "</td><td>"
               << m_totalGestureWidth / m_gestureCount << "</td><td>"
               << m_totalGestureHeight / m_gestureCount << "</td></tr>\n"
               << "</table>\n";

    file.close();
    return true;
}

